package com.lsf.dunlian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.lsf.dunlian.R;
import com.lsf.dunlian.activity.GroupChatActivity;
import com.lsf.dunlian.adapter.SessionRecyclerViewAdapter;
import com.lsf.dunlian.callback.GroupChatCallBack;
import com.lsf.dunlian.config.PreferenceUtil;
import com.lsf.dunlian.config.Urls;
import com.lsf.dunlian.constant.Constant;
import com.lsf.dunlian.databinding.FragmentHomeBinding;
import com.lsf.dunlian.entity.CurrentNoticData;
import com.lsf.dunlian.entity.CurrentNoticEntity;
import com.lsf.dunlian.entity.GroupChatMessageVo;
import com.lsf.dunlian.entity.UserData;
import com.lsf.dunlian.entity.UserVO;
import com.lsf.dunlian.netty.SimpleClientHandler;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HomePageFragment extends Fragment implements GroupChatCallBack {
    private RecyclerView recyclerView;
    private TextView createTime;
    private TextView content;
    private LinearLayout gonggaoView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private SessionRecyclerViewAdapter adapter;
    private List<UserData> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        init(view);
        SimpleClientHandler.register(this);
        return view;
    }

    private void init(View view) {
        recyclerView = view.findViewById(R.id.recyclerView);
        createTime = view.findViewById(R.id.createTime);
        content = view.findViewById(R.id.content);
        gonggaoView = view.findViewById(R.id.gonggaoView);
        swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        adapter = new SessionRecyclerViewAdapter(list, getActivity());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        getNotice();

        getUserList();
        swipeRefreshLayout.setOnRefreshListener(() -> {
            list.clear();
            getUserList();
        });
    }



    private void getUserList() {
        swipeRefreshLayout.setRefreshing(true);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(Urls.USERLIST + "?userId=" + PreferenceUtil.getSharedPreference(Constant.USERID)).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    UserVO vo = new Gson().fromJson(result, UserVO.class);
                    list.addAll(vo.data.content);
                    getActivity().runOnUiThread(() -> adapter.notifyDataSetChanged());
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });
    }


    private void getNotice() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(Urls.NOTICE_CURRENTNOTIC).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    CurrentNoticEntity vo = new Gson().fromJson(result, CurrentNoticEntity.class);
                    getActivity().runOnUiThread(() -> {
                        if (vo.code == 200 && vo.data != null && vo.data.size() > 0) {
                            CurrentNoticData data = vo.data.get(0);
                            gonggaoView.setVisibility(View.VISIBLE);
                            createTime.setText(data.createTime);
                            content.setText(data.content);
                        } else {
                            gonggaoView.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });
    }


    @Override
    public void message(GroupChatMessageVo vo) {
        list.clear();
        getUserList();
    }
}
